package com.woniuxy.creditsystem.financialmag.controller;

import com.github.pagehelper.PageInfo;
import com.woniuxy.creditsystem.common.bean.*;
import com.woniuxy.creditsystem.common.util.ResultMsg;
import com.woniuxy.creditsystem.common.util.TraceUtil;
import com.woniuxy.creditsystem.financialmag.service.IMoneySetInfo;
import com.woniuxy.creditsystem.financialmag.service.IReturnMoneyInfo;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @author ：pengchufeng
 * @date ：Created in 2020/12/14
 * @description：资金方结算信息controller
 */
@RequestMapping("/msinfo")
@RestController
public class MoneySetController {

    @Resource
    private IMoneySetInfo moneySetInfoImpl;

    /**
     * 条件查询所有资金方结算信息
     * @return
     */
    @RequestMapping(value="/select",produces = {"application/json;charset=utf-8"})
    public TableResult<MoneySetInfo> findAll(PageRequest request, MoneySetInfo moneySetInfo){
        Integer sNum = request.getOffset();
        Integer lNum = request.getLimit();
        Integer pNum = request.getPageNum();
        if(sNum == null || sNum < 0){
            sNum = 0;
            request.setOffset(sNum);
        }
        if(lNum == null || lNum <= 0){
            lNum = 10;
            request.setLimit(lNum);
        }
        if(pNum == null || pNum < 0){
            pNum = 1;
            request.setPageNum(pNum);
        }
        PageInfo<MoneySetInfo> pageInfo = moneySetInfoImpl.findAll(request,moneySetInfo);
        List<MoneySetInfo> lp = pageInfo.getList();
        for(int i = 0;i < lp.size();i++){
            MoneySetInfo p1 = lp.get(i);
            p1.setPlatCost(new DecimalFormat("0.00").format(p1.getHandleCost().multiply(p1.getSumMoney())));
            p1.setNewHandleCost(new DecimalFormat("0.00%").format(p1.getHandleCost()));
            p1.setNewSumMoney(new DecimalFormat("0.00").format(p1.getSumMoney()));
            p1.setStrNum(""+p1.getDataNum()+"/"+p1.getPeriods());
            if(p1.getAtime() != null){
                p1.setStrAtime(new SimpleDateFormat("yyyy-MM-dd").format(p1.getAtime()));
            }else{
                p1.setStrAtime("暂无数据");
            }
            if(p1.getAlsoTime() != null){
                p1.setStrDate(new SimpleDateFormat("yyyy-MM-dd").format(p1.getAlsoTime()));
            }else{
                p1.setStrDate("暂无数据");
            }
            lp.set(i,p1);
        }

        return new TableResult<MoneySetInfo>(pageInfo.getTotal(),pageInfo.getList());
    }


    /**
     * 把前台传回来的table中的UserInfo list集合 导出到一个Excel文件
     * @author hejie
     * @param tableDataJson
     * @return ResultMsg
     */
    @RequestMapping(value="/exportList",produces = {"application/json;charset=utf-8"})
    public ResultMsg tableDataJson(List<MoneySetInfo> tableDataJson) throws IOException {
        moneySetInfoImpl.write2excel(tableDataJson,"E:/woniuxy");
        System.out.println("\n"+ TraceUtil.positionTrace());
        for (MoneySetInfo moneySetInfo:tableDataJson) {
            System.out.println(moneySetInfo);
        }
        ResultMsg resultMsg = new ResultMsg(200,"in tableDataJson()",null);
        return resultMsg;
    }

}
